package com.webwork.mood.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.mood.common.controller.SuperCtrl;
import com.webwork.mood.system.dataobject.Job;
import com.webwork.mood.system.service.JobService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Controller
@RequestMapping(value = "/system")
public class JobController extends SuperCtrl {

	@Autowired
	private JobService jobSve;
	
	@RequestMapping(value = "/jobs")
	@Override
	public String mainPage() {
		return "jobs";
	}
	
	@RequestMapping(value = "queryJobsList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Job queryObj = new Job();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Job.class);	
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", jobSve.count(queryObj));
		ret.put("rows", jobSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getJob")
	@ResponseBody
	public ResponseEntity<String> getjob(@RequestParam("jobId") String jobId) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Job job = jobSve.get(jobId);
		return new ResponseEntity<String>(new Gson().toJson(job).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addJob")
	@ResponseBody
	public String addjob(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Job job = new Gson().fromJson(jsonObj, Job.class);
		if (jobSve.get(job.getJobId()) != null) {
			return EXIST;
		}
		jobSve.save(job);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editJob")
	@ResponseBody
	public String editjob(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Job o = new Gson().fromJson(jsonObj, Job.class);
		Job job = jobSve.get(o.getJobId());
		if (job == null) {
			return NOT_EXIST;
		}
		job.setJobName(o.getJobName());
		jobSve.save(job);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delJob")
	@ResponseBody
	public String deljob(@RequestParam("jobId") String jobId) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Job job = jobSve.get(jobId);
		if (job == null) {
			return FAIL;
		}
		jobSve.delete(jobId);
		return SUCCESS;
	}
	
	@RequestMapping(value="loadJobs")
	@ResponseBody
	public ResponseEntity<String> loadJobs() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		List<Job> lists = jobSve.query(new Job());
		List<Object> ret = new ArrayList<Object>();
		for (Job o : lists) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getJobId());
			map.put("name", o.getJobId() + " - " + o.getJobName());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
